#include "EpilRepeatStatementParser.h"
#include "EpilExpressionParser.h"
#include "EpilChunkParser.h"
#include "EpilParserException.h"
#include "EpilPointerHelper.h"


EpilRepeatStatementParser::EpilRepeatStatementParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilRepeatStatementParser::Parse()
{
	// repeatstat -> REPEAT block UNTIL expr
	m_statement = new EpilRepeatStatementCode( this->GetTokens().GetCurrentToken().location );
	this->GetTokens().MoveToNext();

	EpilChunkParser chunkParser( this->GetTokens() );
	try
	{
		chunkParser.Parse();
	}
	catch( ... )
	{
		EpilPointerHelper::SafeDelete( m_statement );
		throw;
	}
	m_statement->SetBlock( chunkParser.GetChunk() );

	if( this->GetTokens().GetCurrentToken().type != ETT_KwUntil )
	{
		EpilPointerHelper::SafeDelete( m_statement );
		throw EpilParserException( t("\'until\' expected."), this->GetTokens().GetCurrentToken().location );
	}
	this->GetTokens().MoveToNext();

	EpilExpressionParser exprParser( this->GetTokens() );
	try
	{
		exprParser.Parse();
	}
	catch( ... )
	{
		EpilPointerHelper::SafeDelete( m_statement );
		throw;
	}
	m_statement->SetCondition( exprParser.GetExpression() );
}